(ns dowloadimg.core
  (:use net.cgrand.enlive-html)
  (:gen-class))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println "Hello, World!"))
(slurp "http://www.baidu.com")


;起始链接
(defn get-s-html [htmlurl]
  (html-resource (java.io.StringReader. (slurp htmlurl))))

;(map #(html-resource (java.io.StringReader. (slurp %))) get-start-url);

;(pprint (select ooxx [:a.view_img_link]))
;获取当前网页所有妹子图片URL
(defn get-img-url [h]
  (map #(:href %) (map #(:attrs %) (select h [:a.view_img_link])))
)

;{:tag :a,
;  :attrs
;  {:class "view_img_link",
;   :target "_blank",
;   :href
;   "http://ww3.sinaimg.cn/large/661eb95cgw1faa2kwwd3ug20a00ayu0y.gif"},
;  :content ("[查看原图]")}
;//下载单个文件
(defn dowloadFile [url]
  (with-open [rd (clojure.java.io/input-stream url) 
              wd  (clojure.java.io/output-stream (str "D://dowloadimg//" (subs url (inc (.lastIndexOf url "/")))))]
    (clojure.java.io/copy rd wd))
)
;下载多个文件
(defn dowlodMFile [coll]
  (pmap #(try
     (dowloadFile %)
     (catch Exception e (println (.getMessage e)))) coll)
  )
        

;URL http://jandan.net/ooxx/page-2251#comments 比较有规律 我们直接用代码生成
;这段代码获取到所有的页面
(defn get-start-url []
  (map #(str "http://jandan.net/ooxx/page-" % "#comments") (range 2255 1 -1))
)

(defn get-m-html [coll]
  (map #(get-s-html %) coll)
  )

(defn get-m-img-url [coll]
  (map #(get-img-url %) coll) 
  )

(defn dowload-end []
  (dowlodMFile (flatten (get-m-img-url (get-m-html (get-start-url)))))
  )

     


     